package view;

import controller.Controller;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * Created by Pavel Nikolaev
 */
public class ControlPanel extends JPanel implements PropertyChangeListener {
    private JButton myBackButton;
    private JButton myForwardButton;
    private JButton myUpdateButton;
    private JTextField myAddressBar;

    public ControlPanel() {
        myBackButton = new JButton("Back");
        myBackButton.setEnabled(false);
        myForwardButton = new JButton("Forward");
        myForwardButton.setEnabled(false);
        myUpdateButton = new JButton("Update");
        myUpdateButton.setEnabled(false);
        myAddressBar = new JTextField();
        myAddressBar.setText("test/test05.html");
        myAddressBar.setCaretPosition(myAddressBar.getText().length());

        GroupLayout layout = new GroupLayout(this);
        setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);

        layout.setHorizontalGroup(layout.createSequentialGroup()
            .addComponent(myBackButton)
            .addComponent(myForwardButton)
            .addComponent(myUpdateButton)
            .addComponent(myAddressBar)
        );

        layout.setVerticalGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                .addComponent(myBackButton)
                .addComponent(myForwardButton)
                .addComponent(myUpdateButton)
                .addComponent(myAddressBar))
        );

        layout.linkSize(SwingConstants.HORIZONTAL, myBackButton, myForwardButton, myUpdateButton);
    }

    public void subscribe(final Controller controller) {
        myAddressBar.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                boolean isBackStepAvailable = controller.readFile(myAddressBar.getText());
                myBackButton.setEnabled(isBackStepAvailable);
                myForwardButton.setEnabled(false);
            }
        });

        myBackButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                boolean isBackStepAvailable = controller.stepBackInHistory();
                myBackButton.setEnabled(isBackStepAvailable);
                myForwardButton.setEnabled(true);
            }
        });

        myForwardButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                boolean isForwardStepAvailable = controller.stepForwardInHistory();
                myForwardButton.setEnabled(isForwardStepAvailable);
                myBackButton.setEnabled(true);
            }
        });

        myUpdateButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                controller.updateDocument();
            }
        });
    }

    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("DocumentUpdated")) {
            myUpdateButton.setEnabled(true);
        }
    }
}
